import { useUserStore } from '@/stores/user'
import axios from 'axios'

// reverse proxy
const baseURL = '/api'

const instance = axios.create({
    baseURL,    // base url
    timeout: 100000 // time-out period
})

instance.interceptors.request.use(
    (config) => {
      // A request that includes an Authorization header with a user's ID 
      //  confirms that the user is logged in and identifies who the user is.
      const userStore = useUserStore()
      if(userStore.userId){
        config.headers.Authorization = userStore.userId
      }
      return config
    },
    (err) => Promise.reject(err)
  )
  
  instance.interceptors.response.use(
    (res) => {
      // TODO Handle Error
      return res
    },
    (err) => {
      // TODO Handle 401 
      return Promise.reject(err)
    }
  )

export { baseURL }
export default instance


